package edu.pitt.dbmi.odie.gapp.gwt.client.util.panel;

import java.util.List;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FormLayoutType;
import com.smartgwt.client.widgets.events.ResizedEvent;
import com.smartgwt.client.widgets.events.ResizedHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;

public class ODIE_ButtonItemPanel extends DynamicForm {
	
	private boolean hasResized = false ;
	
	public ODIE_ButtonItemPanel(String title, List<ButtonItem> buttonItems) {
		
		this.setGroupTitle(title);
		this.setIsGroup(true);

		this.setCellBorder(0);
		this.setCellPadding(2);
		this.setCellSpacing(0);
		this.setAlign(Alignment.CENTER);
		this.setMinColWidth(1);
		this.setFixedColWidths(false);
		this.setTitleWidth(0);
		this.setItemLayout(FormLayoutType.TABLE);
		this.setNumCols(buttonItems.size());
		this.setWidth("100%") ;
		this.setColWidths(150);
		this.setAlign(Alignment.CENTER) ;
		
		if (buttonItems.size()>0) {
			final ButtonItem[] buttonItemArray = new ButtonItem[buttonItems.size()] ;
			final Object[] buttonWidthArray = new Object[buttonItems.size()] ;
			int idx = 0 ;
			for (ButtonItem b : buttonItems) {
				b.setAlign(Alignment.CENTER) ;
				buttonWidthArray[ idx ] = "*" ;
				buttonItemArray[ idx++ ] = b ;
			}
			this.setColWidths(buttonWidthArray) ;
			this.setItems(buttonItemArray) ;
		}
		
		addResizedHandler(new ResizedHandler() {
			public void onResized(ResizedEvent event) {
				if (ODIE_ButtonItemPanel.this.hasResized) {
					redraw() ;	
				}
				else {
					ODIE_ButtonItemPanel.this.hasResized = true ;
				}
				
			}}) ;	
	}
	
	public static ButtonItem buildButtonItem(String title) {
		ButtonItem buttonItem = new ButtonItem() ;
		buttonItem.setTitle(title) ;
		buttonItem.setAlign(Alignment.CENTER) ;
		buttonItem.setTitleAlign(Alignment.CENTER) ;
		buttonItem.setWidth("15%") ;
		buttonItem.setWidth(100) ;
//		buttonItem.setHeight("*") ;
		buttonItem.setStartRow(false) ;
		buttonItem.setEndRow(false) ;
		return buttonItem ;
	}
	
}
